//
//  YZYVisitorView.swift
//  weibo
//
//  Created by mac on 16/10/27.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class YZYVisitorView: UIView {
    // 01 监听访客视图 view 点击回调
    var closure: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 供外界调用的方法
    func setupVisitorViewInfo(imgName: String?, title: String?) {
        if let img = imgName, let title = title {
            // 设置image 和text
            iconImageView.image = UIImage(named: img)
            messageLabel.text = title
            // 隐藏圆圈
            circleImageView.isHidden = true
        } else {
            // 首页 不需要设置
            setupCircleImageVieAnim()
        }
    }
    // 设置圆圈动画
    private func setupCircleImageVieAnim() {
        // 核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置转的角度
        anim.toValue = M_PI * 2
        // 设置时间
        anim.duration = 20
        // 重复次数
        anim.repeatCount = MAXFLOAT
        // 如果你切换控制器 或者程序推到后台 默认情况下动画会被移除
        anim.isRemovedOnCompletion = false
        // 添加动画
        circleImageView.layer.add(anim, forKey: nil)
    }
    
    // MARK: - 监听事件
    @objc private func loginClick() {
        // 03 执行闭包
        closure?()
    }
    
    // MARK: - 设置视图
    private func setupUI() {
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        // 添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        // 添加约束
        // 圆圈
        circleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        // 遮罩
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        // 图片
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        // 文字
        messageLabel.snp_makeConstraints { (make) in
            make.width.equalTo(230)
            make.centerX.equalTo(self)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
        }
        // 注册按钮
        registerButton.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 35))
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
        // 登录按钮
        loginButton.snp_makeConstraints { (make) in
            make.size.top.equalTo(registerButton)
            make.right.equalTo(messageLabel)
        }
    }
    
    // MARK: - 懒加载动画
    // 圆圈
    fileprivate lazy var circleImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    // 遮罩
    fileprivate lazy var maskImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    // 图片
    fileprivate lazy var iconImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    // 文字
    fileprivate lazy var messageLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.darkGray, fontSize: YZYNormalFontSize, text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
        // 对其方式
        lab.textAlignment = .center
        // 换行
        lab.numberOfLines = 0
        return lab
    }()
    
    // 注册按钮
    fileprivate lazy var registerButton: UIButton = {
        let button = UIButton(setBackgroundImgName: "common_button_white", title: "注册", fontSize: YZYNormalFontSize, titleColor: YZY_Color, target: self, action: #selector(loginClick))
        return button
    }()
    // 登录按钮
    fileprivate lazy var loginButton: UIButton = {
        let button = UIButton(setBackgroundImgName: "common_button_white", title: "登录", fontSize: YZYNormalFontSize, titleColor: YZY_Color, target: self, action: #selector(loginClick))
        return button
    }()
    
}
